package com.motu.monstercity.server.game.logichandler;

import com.motu.monstercity.module.common.network.RequestParam;
import com.motu.monstercity.protocol.Common.PBUserData;
import com.motu.monstercity.protocol.CsGameAct;
import com.motu.monstercity.protocol.CsGameOnceact.*;
import com.motu.monstercity.server.game.base.ProtoDataUtils;
import com.motu.monstercity.server.game.commondata.act.ActConstant;
import com.motu.monstercity.server.game.commondata.act.ActTask;
import com.motu.monstercity.server.game.commondata.system.SystemConstant;
import com.motu.monstercity.server.game.log.LogType;
import com.motu.monstercity.server.game.logic.*;
import com.motu.monstercity.server.game.userdata.UserActDiscountGift;
import com.motu.monstercity.server.game.userdata.UserActTaskReward;
import com.motu.monstercity.server.game.userdata.UserInfo;
import com.motu.monstercity.server.game.verticle.MainVerticle;
import com.motu.vertx.module.utility.base.ServerConfig;
import com.motu.vertx.module.utility.main.MainService;
import com.motu.vertx.module.utility.toolset.ErrorWord;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.json.JsonArray;


public class OnceactHandler {
    // CMD_GAME_ONCEACT_MAIN_INFO = 72301; // 一次性活动主界面，下发玩家领取记录等数据，有都市等级福利
    public static Object CMD_GAME_ONCEACT_MAIN_INFO(RequestParam clientParam) throws Exception {
        CSGameOnceactMainInfoRequest request = CSGameOnceactMainInfoRequest.parseFrom(clientParam.getData());
        CSGameOnceactMainInfoResponse.Builder response = CSGameOnceactMainInfoResponse.newBuilder();
        long userId = clientParam.getUserId();

        PBUserData.Builder pbUserData = response.getUserBuilder();

        UserActTaskReward userActTaskReward = OnceActManager.getUserActTastReward(userId, ActConstant.ACT_TASK_GROUP_ID_CITY);// 都市等级福利领取情况
        ProtoDataUtils.updatePBUserData(pbUserData, userActTaskReward, true);






        return response;
    }

    // CMD_GAME_ONCEACT_CITY_LEVEL_GET_REWARD = 72302; // 领取都市等级福利
    public static Object CMD_GAME_ONCEACT_CITY_LEVEL_GET_REWARD(RequestParam clientParam) throws Exception {
        CSGameOnceactCityLevelGetRewardRequest request = CSGameOnceactCityLevelGetRewardRequest.parseFrom(clientParam.getData());
        CSGameOnceactCityLevelGetRewardResponse.Builder response = CSGameOnceactCityLevelGetRewardResponse.newBuilder();
        long userId = clientParam.getUserId();

        int level = request.getLevel();
        if (!ServerConfig.isLocalServer() && level <= 0) {
            return ErrorWord.BAD_PARAM;
        }
        ActTask actTask = null;
        if (level > 0) {
            actTask = ActDaysManager.getActTaskCity(level);
            if (actTask == null) {
                return ErrorWord.NOT_DATA;// 未配置数据
            }
        }

        UserInfo userInfo = GameUser.getUserInfo(userId);
        if (level > 0 && userInfo.getLevel() < level) {
            return ErrorWord.ITEM_LACK;// 等级不足
        }

        int taskGroupId = ActConstant.ACT_TASK_GROUP_ID_CITY;
        UserActTaskReward userActTaskReward = OnceActManager.getUserActTastReward(userId, taskGroupId);
        JsonArray jsonArray = new JsonArray();
        if (userActTaskReward != null) {
            jsonArray = userActTaskReward.getRewardInfoArray();
            if (level > 0 &&Tool.isInList(jsonArray, level)) {
                return ErrorWord.REPEATED_OPERATION;// 已经领取过了
            }
        }

        // 执行领取逻辑
        PBUserData.Builder pbUserData = response.getUserBuilder();

        if (level > 0) {
            RewardManager.addReward(userInfo, actTask.getRewardItemList(), pbUserData, LogType.ACT_CITY_LEVEL_REWARD);
            jsonArray.add(level);
        } else {
            for (ActTask actTaskConfig : ActDaysManager.getActTaskMap().values()) {
                if (actTaskConfig.getTaskGroupId() == taskGroupId && !Tool.isInList(jsonArray, actTaskConfig.getNum1())) {
                    if (actTaskConfig.getNum1() <= userInfo.getLevel()) {
                        RewardManager.addReward(userInfo, actTaskConfig.getRewardItemList(), pbUserData, LogType.ACT_CITY_LEVEL_REWARD);
                        jsonArray.add(actTaskConfig.getNum1());
                    }
                }
            }
        }
        if (userActTaskReward == null) {
            userActTaskReward = new UserActTaskReward();
            userActTaskReward.putUserId(userId);
            userActTaskReward.putTaskGroupId(taskGroupId);
            userActTaskReward.putRewardInfo(jsonArray.toString());
            userActTaskReward.doCreate();
            ProtoDataUtils.updatePBUserData(pbUserData, userActTaskReward, true);
        } else {
            userActTaskReward.putRewardInfo(jsonArray.toString());
            userActTaskReward.update();
            ProtoDataUtils.updatePBUserData(pbUserData, userActTaskReward);
        }
        if (OnceActManager.checkCityLeveReward(taskGroupId, userActTaskReward)) {// 已经全部领取+
            pbUserData.getOnceActBuilder().getCityLevelRewardBuilder().setIsComplete(true);
        }

        return response;
    }
}
